package com.wxb.boot.gateway.predicate;

import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * describe:
 *
 * @author : wxbing
 * @date : 2021/4/2
 */
@Component
@Order(1)
public class AimRoutePredicateFactory extends AbstractRoutePredicateFactory<AimRoutePredicateFactory.Config> {

	public AimRoutePredicateFactory() {
		super(AimRoutePredicateFactory.Config.class);
	}

	@Override
	public Predicate<ServerWebExchange> apply(Consumer<Config> consumer) {
		return null;
	}

	@Override
	public Predicate<ServerWebExchange> apply(Config config) {

		return exchange -> {
			String requestPath = exchange.getRequest().getURI().getPath();
			String original = config.getOriginal();
			if (requestPath.equals(original)) {
				return true;
			}
			return false;
		};
	}

	@Override
	public String name() {
		return "redirectAimUrlPredicate";
	}


	public static class Config {

		private String original;

		public String getOriginal() {
			return original;
		}

		public void setOriginal(String original) {
			this.original = original;
		}
	}
}
